﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Portal.Framework;
using RSHB.Framework.Data;

namespace RSHB.Framework.BLL
{
    public class LoanAgreementBuilder
    {
        int _loanAgreementID;
        public LoanAgreementBuilder(int loanAgreementID)
        {
            this._loanAgreementID = loanAgreementID;
        }

        public string Build()
        {
            string fileContent = string.Empty;

            using (TextReader tr = new StreamReader(Path.Combine(PortalContext.FileTemplatesPath, "Template.rtf"), false ))
            {
                fileContent = tr.ReadToEnd();                             
            }

            System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox();
            rtb.Rtf = fileContent;


            LoanAgreement loanAgreement = (new BHDDataContext()).LoanAgreements.Where(l => l.LoanAgreementID == this._loanAgreementID).Single();

            rtb.Rtf = rtb.Rtf.Replace("BorrowerNameLast", loanAgreement.Borrower.VCards.Single().vcardNameLast);
            rtb.Rtf = rtb.Rtf.Replace("BorrowerNameFirst", loanAgreement.Borrower.VCards.Single().vcardNameFirst);
            rtb.Rtf = rtb.Rtf.Replace("BorrowerNameMiddle", loanAgreement.Borrower.VCards.Single().vcardNameMiddle);
            return rtb.Rtf;
        }
    }
}
